package com.ewzj.bigdata.mvp.ui.activity;

import android.animation.Animator;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.AndroidBug5497Workaround;
import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.di.component.DaggerCommentListComponent;
import com.ewzj.bigdata.di.module.CommentListModule;
import com.ewzj.bigdata.mvp.contract.CommentListContract;
import com.ewzj.bigdata.mvp.model.entity.resbean.ArticleBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.CommentBean;
import com.ewzj.bigdata.mvp.presenter.CommentListPresenter;
import com.ewzj.bigdata.mvp.ui.adapter.CommentAdapter;
import com.ewzj.bigdata.mvp.ui.widget.CommentListTextView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class CommentListActivity extends BaseActivity<CommentListPresenter> implements CommentListContract.View {
    @BindView(R.id.rv_comments)
    RecyclerView rvComments;
    @BindView(R.id.rl_writecomment)
    RelativeLayout rlWritecomment;
    @BindView(R.id.et_comment)
    EditText etComment;
    @BindView(R.id.iv_fullscreen)
    ImageView ivFullscreen;
    @BindView(R.id.ll_btn)
    RelativeLayout llBtn;
    @BindView(R.id.iv_cover)
    View ivCover;
    @BindView(R.id.rl_comment)
    RelativeLayout rlComment;
    @BindView(R.id.rl_pcomment)
    RelativeLayout rlPcomment;
    private ArticleBean articleBean;
    private List<CommentBean> commentBeans;
    private CommentAdapter commentAdapter;
    private CommentBean replyComment;
//    1表示评论模式，2表示回复模式
    private int commentMode=1;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerCommentListComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .commentListModule(new CommentListModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_comment_list; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        AndroidBug5497Workaround.assistActivity(this);
        articleBean = getIntent().getParcelableExtra(AppConstants.EXTRA_DATA);
        if (articleBean == null) {
            killMyself();
        }
        initAdapter();
        mPresenter.loadCommentListData();
    }

    private void initAdapter() {
        if (commentBeans == null) {
            commentBeans = new ArrayList<>();
        }
        if (commentAdapter == null) {
            commentAdapter = new CommentAdapter(commentBeans);
        }
        rvComments.setLayoutManager(new LinearLayoutManager(this));
        rvComments.setAdapter(commentAdapter);
        commentAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMoreCommentListData();
            }
        }, rvComments);
        commentAdapter.disableLoadMoreIfNotFullPage();
        commentAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.ll_like:
                        ((CommentBean) adapter.getData().get(position)).setIs_praise(true);
                        adapter.notifyItemChanged(position);
                        mPresenter.commentPraise(((CommentBean) adapter.getData().get(position)).getId()+"");
                        break;
                    case R.id.ll_comment:
                        commentMode=2;
                        replyComment= (CommentBean) adapter.getData().get(position);
                        showCommentView();
                        break;
                }
            }
        });
        commentAdapter.setCommentListener(new CommentAdapter.CommentPartClickListener() {
            @Override
            public void onNickNameClick(int position, CommentListTextView.CommentInfo mInfo) {
                ArmsUtils.snackbarText(mInfo.getNickname());
            }

            @Override
            public void onToNickNameClick(int position, CommentListTextView.CommentInfo mInfo) {
                ArmsUtils.snackbarText(mInfo.getTonickname());
            }

            @Override
            public void onCommentItemClick(int position, CommentListTextView.CommentInfo mInfo) {
                ArmsUtils.snackbarText(mInfo.getComment());
            }
        });
//        commentAdapter.
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void notifyDataAdd(List<CommentBean> comments) {
        commentAdapter.addData(comments);
    }

    @Override
    public void refresh(List<CommentBean> comments) {
        commentAdapter.setNewData(comments);
    }

    @Override
    public ArticleBean obtainArticleBean() {
        return articleBean;
    }


    @OnClick({R.id.tv_writecomment,R.id.iv_cover,R.id.iv_close,R.id.tv_send,R.id.iv_fullscreen})
    public void onViewClicked(View view) {
        String comment=etComment.getText().toString();
        switch (view.getId()){
            case R.id.tv_writecomment:
                commentMode=1;
               showCommentView();
                break;
            case R.id.iv_cover:
                hideCommentView();
                break;
            case R.id.iv_close:
                killMyself();
                break;
            case R.id.tv_send:
                if (Apputils.isLogin(this)){
                    if (TextUtils.isEmpty(comment)){
                        ArmsUtils.snackbarText("评论内容不能为空");
                        return;
                    }
                    String userid= DataHelper.getStringSF(this,AppConstants.USERID);
                    if (commentMode==1){
                        mPresenter.sendComment(comment,articleBean.getId()+"",userid);
                    }else {
                        mPresenter.sendReplyComment(comment,replyComment.getId()+"",userid);
                    }
                }else {
                    ArmsUtils.startActivity(LoginActivity.class);
                }

                break;
            case R.id.iv_fullscreen:
                ArmsUtils.startActivity(EditFullScreenActivity.class);
                break;
        }

    }
    public void showCommentView(){
        rlPcomment.setVisibility(View.VISIBLE);
        etComment.setText("");
        YoYo.with(Techniques.FadeInUp)
                .repeat(0)
                .withListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        ivCover.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {

                    }
                })
                .duration(500)
                .playOn(rlComment);
    }
    private void hideCommentView(){
        DeviceUtils.hideSoftKeyboard(this,etComment);
        YoYo.with(Techniques.FadeOutDown)
                .repeat(0)
                .withListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {
                        ivCover.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        rlPcomment.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {

                    }
                })
                .duration(500)
                .playOn(rlComment);
    }

    @Override
    public void commentSendSuccess() {
        hideCommentView();
        mPresenter.loadCommentListData();
    }

    @Override
    public CommentAdapter obtainCommentAdapter() {
        if (commentAdapter==null){
            commentAdapter=new CommentAdapter(new ArrayList<>());
        }
        return commentAdapter;
    }

    @Override
    public void PraiseSuccess(String id) {
        ArmsUtils.snackbarText("点赞成功");
//        mPresenter.loadCommentListData();
    }
}
